home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / Random / Confusion / source / main.c < prev    next >
C/C++ Source or Header  |  1992-09-18  |  7KB  |  272 lines

  1. /*------------------------------------------------------------------------------
  2. # Main for Confusion
  3. #
  4. #    Copyright © 1992 Bernard Bernstein
  5. #
  6. #    taken from some background-only sample code and other sample code shells
  7. ------------------------------------------------------------------------------*/
  8.  
  9. #include <memory.h>
  10. #include <appleevents.h>
  11. #include <events.h>
  12. #include <types.h>
  13. #include <gestaltequ.h>
  14.  
  15. #include <SoundInput.h>
  16. #include <Sound.h>
  17.  
  18. #include "Confusion.h"
  19.  
  20. /* variables */
  21. Boolean    gInBackground;
  22. Boolean gQuit = false;
  23. EventRecord gERecord;
  24. Boolean gHasAppleEvents;
  25. unsigned long gMySleep = 30;
  26.  
  27. struct AEinstalls {
  28.     AEEventClass theClass;
  29.     AEEventID theEvent;
  30.     EventHandlerProcPtr theProc;
  31. };
  32. typedef struct AEinstalls AEinstalls;
  33.  
  34. /* prototypes for this file */
  35. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  36. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  37. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  38. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  39. void DoHighLevel(EventRecord *AERecord);
  40. void InitAEStuff(void);
  41. void DoEvent(EventRecord *event);
  42. void DoMenuCommand(long menuResult);
  43. void AdjustMenus( void );
  44. void Initialize(void);
  45.  
  46. main()
  47. {
  48.     Boolean gotEvent;
  49.     
  50.     Initialize();
  51.     InitSoundConfusion();
  52.    
  53.     while (gQuit == false) {
  54.         gotEvent = WaitNextEvent(everyEvent, &gERecord, gMySleep, 0);
  55.         if (gotEvent)
  56.             {
  57.             DoEvent(&gERecord);
  58.             }
  59.         ConfuseSound();
  60.     }
  61.     StopConfusion();
  62.     ExitToShell();
  63. }
  64.  
  65. void DoEvent(EventRecord *event)
  66. {
  67.     short        part;
  68.     WindowPtr    window;
  69.     Boolean        hit;
  70.     char        key;
  71.  
  72.     switch ( event->what ) {
  73.         case mouseDown:
  74.             part = FindWindow(event->where, &window);
  75.             switch ( part ) {
  76.                 case inMenuBar:                /* process a mouse menu command (if any) */
  77.                     AdjustMenus();
  78.                     DoMenuCommand(MenuSelect(event->where));
  79.                     break;
  80.                 case inSysWindow:            /* let the system handle the mouseDown */
  81.                     SystemClick(event, window);
  82.                     break;
  83.             }
  84.             break;
  85.         case keyDown:
  86.         case autoKey:                        /* check for menukey equivalents */
  87.             key = event->message & charCodeMask;
  88.             if ( event->modifiers & cmdKey )            /* Command key down */
  89.                 if ( event->what == keyDown ) {
  90.                     AdjustMenus();                        /* enable/disable/check menu items properly */
  91.                     DoMenuCommand(MenuKey(key));
  92.                 }
  93.             break;
  94.         case osEvt:
  95.             switch ((event->message >> 24) & 0x0FF) {        /* high byte of message */
  96.                 case suspendResumeMessage:        /* suspend/resume is also an activate/deactivate */
  97.                     gInBackground = (event->message & resumeFlag    ) == 0;
  98.                     break;
  99.             }
  100.             break;
  101.         case kHighLevelEvent:
  102.             if (gHasAppleEvents)
  103.                 DoHighLevel(&gERecord);
  104.             break;
  105.     }
  106. } /*DoEvent*/
  107.  
  108.  
  109. void DoMenuCommand(long menuResult)
  110. {
  111.     short        menuID;                /* the resource ID of the selected menu */
  112.     short        menuItem;            /* the item number of the selected menu */
  113.     short        itemHit;
  114.     Str255        daName;
  115.     short        daRefNum;
  116.     Boolean        handledByDA;
  117.  
  118.     menuID = HiWord(menuResult);    /* use macros for efficiency to... */
  119.     menuItem = LoWord(menuResult);    /* get menu item number and menu number */
  120.     switch ( menuID ) {
  121.         case mApple:
  122.             switch ( menuItem ) {
  123.                 case iAbout:        /* bring up alert for About */
  124.                     DoAbout();
  125.                     break;
  126.                 default:            /* all non-About items in this menu are DAs */
  127.                     GetItem(GetMHandle(mApple), menuItem, daName);
  128.                     daRefNum = OpenDeskAcc(daName);
  129.                     break;
  130.             }
  131.             break;
  132.         case mFile:
  133.             switch ( menuItem ) {
  134.                 case iQuit:
  135.                     gQuit = true;
  136.                     break;
  137.             }
  138.             break;
  139.         case mEdit:                    /* call SystemEdit for DA editing & MultiFinder */
  140.             handledByDA = SystemEdit(menuItem-1);    /* since we don’t do any Editing */
  141.             break;
  142.     }
  143.     HiliteMenu(0);                    /* unhighlight what MenuSelect (or MenuKey) hilited */
  144. } /*DoMenuCommand*/
  145.  
  146.  
  147. void AlertUser(OSErr err, short errID)
  148. {
  149.     short        itemHit;
  150.     Str255        errStr;
  151.     Str255        description;
  152.     long        lErr = (long)err;
  153.     DialogPtr    dlg;
  154.     
  155.     SetCursor(&qd.arrow);
  156.     GetIndString(description, rErrStrings, errID);
  157.     NumToString(lErr, errStr);
  158.     
  159.     ParamText(errStr, description, "", "");
  160.     dlg = GetNewDialog(rErrAlert, NULL, (WindowPtr)-1L);
  161.     ModalDialog(NULL, &itemHit);
  162.     DisposDialog(dlg);
  163.     
  164.     gQuit = true;
  165. } /* AlertUser */
  166.  
  167.  
  168. void AdjustMenus( void )
  169. {
  170.  
  171. }
  172.  
  173.  
  174. void Initialize()
  175. {
  176.     Handle        menuBar;
  177.     EventRecord event;
  178.     short        count;
  179.  
  180.     gInBackground = false;
  181.  
  182.     InitGraf((Ptr) &qd.thePort);
  183.     InitFonts();
  184.     InitWindows();
  185.     InitMenus();
  186.     TEInit();
  187.     InitDialogs(nil);
  188.     InitCursor();
  189.  
  190.     for (count = 1; count <= 3; count++)
  191.         EventAvail(everyEvent, &event);
  192.  
  193.     MaxApplZone();
  194.     
  195.     menuBar = GetNewMBar(rMenuBar);            /* read menus into menu bar */
  196.     SetMenuBar(menuBar);                    /* install menus */
  197.     DisposHandle(menuBar);
  198.     AddResMenu(GetMHandle(mApple), 'DRVR');    /* add DA names to Apple menu */
  199.     DrawMenuBar();
  200.     
  201.     InitAEStuff();
  202. }
  203.  
  204.  
  205. /* InitAEStuff checks for the availability of the AppleEvent Manager and */
  206. /* installs our event handlers. */
  207. /* if the AEM isn't around, we bail. */
  208. void InitAEStuff(void)
  209. {
  210.     static AEinstalls HandlersToInstall[] =  {
  211.          {
  212.             kCoreEventClass, kAEOpenApplication, AEOpenHandler
  213.         },  {
  214.             kCoreEventClass, kAEOpenDocuments, AEOpenDocHandler
  215.         },  {
  216.             kCoreEventClass, kAEQuitApplication, AEQuitHandler
  217.         },  {
  218.             kCoreEventClass, kAEPrintDocuments, AEPrintHandler
  219.         }, 
  220.         /* The above are the four required AppleEvents. */
  221.     };
  222.     
  223.     OSErr aevtErr = noErr;
  224.     long aLong = 0;
  225.     Boolean gHasAppleEvents = false;
  226.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
  227.     if (gHasAppleEvents) {
  228.         register qq;
  229.         for (qq = 0; qq < ((sizeof(HandlersToInstall) / sizeof(AEinstalls))); qq++) {
  230.             aevtErr = AEInstallEventHandler(HandlersToInstall[qq].theClass, HandlersToInstall[qq].theEvent,
  231.                                             HandlersToInstall[qq].theProc, 0, false);
  232.             if (aevtErr) {
  233.                 AlertUser(aevtErr, iAEErr);
  234.             }
  235.         }
  236.     }
  237. }
  238. /* end InitAEStuff */
  239.  
  240. void DoHighLevel(EventRecord *AERecord)
  241. {
  242.     AEProcessAppleEvent(AERecord);
  243. }
  244.  
  245. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  246. {
  247. #pragma unused (messagein,reply,refIn)
  248.     return(noErr);
  249. }
  250.  
  251. /* end AEOpenHandler */
  252.  
  253. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  254. {
  255. #pragma unused (reply, refIn,messagein)
  256.     return(errAEEventNotHandled);
  257. }
  258.  
  259. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  260. {
  261. #pragma unused (reply,refIn,messagein)
  262.     return(errAEEventNotHandled);
  263. }
  264.  
  265. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  266. {
  267. #pragma unused (messagein,refIn,reply)
  268.     extern Boolean Quit;
  269.     gQuit = true;
  270.     return(noErr);
  271. }
  272.